# A token that is created by the lexer and can be recognized by the parser.
#
# @author Paul T.
class Calcula::Token

  # @return [Symbol] Returns the type of the token
  attr_reader :id

  # @return [String] Returns the string segment from the source
  attr_reader :text

  # @return [Fixnum] Returns the line of the token's position in the source code
  attr_reader :lineNum

  # @return [Fixnum] Returns the column of the token's position in the source code
  attr_reader :linePos

  # Constructs a new Token that can be used with `Calcula::Parser`
  #
  # @param id [Symbol] The type of the Token
  # @param text [String] The actual text
  # @param lineNum [Fixnum] The line number of the token's position in the source code
  # @param linePos [Fixnum] The column number of the token's position in the source code
  def initialize(id, text, lineNum: -1, linePos: -1)
    @id = id
    @text = text
    @lineNum = lineNum
    @linePos = linePos
  end

  # Converts the token into a format of `[:id => :text]`
  #
  # @return [String] The string representation of the Token in above format
  def to_s
    return "[#{@id}(L#{@lineNum}:#{@linePos})=>#{text}]"
  end

  # Duplicates a token
  #
  # @return [Calcula::Token] A duplicate of the token
  def dup
    Token.new(@id, @text)
  end
end
